package arraylist;

import java.util.ArrayList;
/*
*ArrayList<E>中的方法:
*   public boolean add(E e);
*   public void add(int index, E element):将指定元素添加到指定索引位置,该位置原有元素以及该位置之后的元素向后移
*   public E get(int index):将指定索引位置的元素取出
*   public E set(int index, E element):将指定索引位置的元素设置成新的值,返回该索引位置的旧的值
*   public E remove(int index):将指定索引位置的元素删除,返回删除的元素
* */
public class ArrayListDemo01 {
    public static void main(String[] args) {
        //method01();
        //method02();
        //method03();
        //method04();
        method05();
    }

    private static void method05() {
        ArrayList<String> al = new ArrayList<>();
        al.add("无忌");
        al.add("三丰");
        al.add("王敏");

        System.out.println(al);

        String str = al.remove(1);
        System.out.println(str);

        System.out.println(al);
    }

    private static void method04() {
        ArrayList<String> al = new ArrayList<>();
        al.add("无忌");
        al.add("三丰");
        al.add("王敏");
        System.out.println(al);

        String str = al.set(1, "灭绝师太");//返回的是指定索引位置的旧元素
        System.out.println(str);//三丰

        System.out.println(al);//[无忌, 灭绝师太, 王敏]
    }

    private static void method03() {
        ArrayList<String> al = new ArrayList<>();
        al.add("无忌");
        al.add("三丰");
        al.add("王敏");
        String s = al.get(1);
        System.out.println(s);
        System.out.println(al.get(0));
    }

    private static void method02() {
        ArrayList<String> al = new ArrayList<>();
        al.add("abc");
        al.add("ghk");
        System.out.println(al);//[abc, ghk]

        al.add(1, "bb");
        System.out.println(al);//[abc, bb, ghk]

        al.add(0, "00");
        System.out.println(al);//[00, abc, bb, ghk]
    }

    private static void method01() {
        ArrayList<String> al = new ArrayList<>();
        al.add("abc");
        al.add("def");
        al.add("ghk");
        System.out.println(al);
    }
}
